package com.rodvl.core.repository;

import com.rodvl.common.orm.Limitable;
import com.rodvl.core.domain.User;
import com.rodvl.core.repository.plus.UserDaoPlus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;

import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @author lzb22
 */
public interface UserDao extends JpaRepository<User, Integer>, UserDaoPlus {
     public Page<User> findAll(Specification<User> spec, Pageable pageable);

     public List<User> findAll(Specification<User> spec, Limitable limitable);

    public User getById(Integer id);

    public User save(User bean);

    /**
     * 查询注册用户数量
     *
     * @param beginDate 开始日期
     * @return
     */
    @Query("select count(*) from UserDetail bean where bean.creationDate >= ?1")
    public long countByDate(Date beginDate);

    public User findByUsername(String username);

    public User findByMobile(String username);

    public User findByEmail(String username);

    public User findByValidationTypeAndValidationKey(String type, String key);

    @Query("select count(*) from User bean where bean.username=?1")
    public long countByUsername(String username);

    @Query("select count(*) from User bean where bean.org.id in ?1")
    public long countByOrgId(Collection<Integer> orgIds);

    @Query("select count(*) from User bean where bean.group.id in ?1")
    public long countByGroupId(Collection<Integer> groupIds);
}
